/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.dao.impl;

import au.edu.unsw.bookmarket.actionform.BookBean;
import au.edu.unsw.bookmarket.dao.BookDao;
import au.edu.unsw.bookmarket.jdo.Book;
import au.edu.unsw.bookmarket.service.PMF;
import au.edu.unsw.bookmarket.util.BeanMapping;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 *
 * @author gongz
 */
public class BookDaoImpl implements BookDao {

    public boolean addBookAsBookBean(BookBean bookBean) {
        boolean success = false;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        BeanMapping beanMapping = new BeanMapping();
        try {
            Book book = beanMapping.bookBean2Book(bookBean);
            pm.makePersistent(book);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pm.close();
        }
        return success;
    }

    public boolean addBook(Book book) {
        boolean success = false;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(book);
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pm.close();
        }
        return success;
    }

    public BookBean bookExistency(BookBean bookBean) {
        BookBean book = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        BeanMapping beanMapping = new BeanMapping();
        Query query = pm.newQuery(Book.class);
        try {
            query.setFilter("isbn == inputISBN");
            query.declareParameters("String inputISBN");
            List<Book> results = (List<Book>) query.execute(bookBean.getIsbn().trim().toLowerCase());
            if (results.size() > 0) {
                System.out.println("found ISBN: " + bookBean.getIsbn());
                return beanMapping.book2BookBean(results.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return book;
    }

    public List<BookBean> getAllBooks() {
        List<BookBean> bookBeans = new ArrayList<BookBean>();
        BeanMapping beanMapping = new BeanMapping();
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Book.class);
        try {
            List<Book> books = (List<Book>) query.execute();
            for (Book book : books) {
                bookBeans.add(beanMapping.book2BookBean(book));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return bookBeans;
    }

    public boolean removeAllBooks() {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Book.class);
        boolean success = false;
        try {
            query.deletePersistentAll();
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return success;
    }
}
